package com.k.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.k.demo.entity.CustomerUser;
import com.k.demo.entity.FoodKind;
import com.k.demo.service.FoodKindService;

@RestController
@RequestMapping("/foodkind")
public class FoodKindController {

	@Autowired
	private FoodKindService foodKindService;
	
	/**
	 * 返回全部菜品分类
	 * url /foodkind/all
	 * @author zzg
	 */
	@RequestMapping("/all")
	public Object all() {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("foodkindList", foodKindService.getAll());
		
		return map;
	}
	
	@RequestMapping("/all/page")
	public Object allByPage(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		PageHelper.startPage((int)map.get("pageNum"), (int)map.get("pageSize"));
		
		List<FoodKind> foodkindlist = foodKindService.getAll();
		
		PageInfo pageinfo =new PageInfo<>(foodkindlist,(int)map.get("pageSize"));
		
		rmap.put("foodkindList", pageinfo);
		
		return rmap;
	}
	//此处的foodkind只需要food_kind_depiction和food_kind_name
	@RequestMapping("/add")
	public Object addFoodKind(@RequestBody FoodKind foodkind) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("insertnum", foodKindService.addFoodKind(foodkind));
		
		return map;
	}
	@RequestMapping("/deleteforeach")
	public Object deleteFoodKindForEach(@RequestBody String deletelist) {
		
		JSONObject jsonObject = JSON.parseObject(deletelist);
		
		List<Integer> delete_list = JSON.parseArray(jsonObject.getString("delete_list"), Integer.class);
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("deletenum", foodKindService.deleteFoodKindForEach(delete_list));
		
		return map;
	}
	
	@RequestMapping("/delete")//带id
	public Object delete(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("deletenum",foodKindService.delete((int)map.get("food_kind_id")));
		
		return rmap;
	}
	
	@RequestMapping("/update")//带id
	public Object updateFoodKind(@RequestBody FoodKind foodkind) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("updatenum", foodKindService.updateFoodKind(foodkind));
		
		return map;
	}
	@RequestMapping("/searchbyidandname")
	public Object getByIdAndName(@RequestBody FoodKind foodkind) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("foodkindList", foodKindService.getByNameAndId(foodkind));
		
		return rmap;
	}
	
	@RequestMapping("/searchbyname/page")
	public Object getByName(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		
		FoodKind foodkind= new FoodKind();
		foodkind.setFood_kind_name((String)map.get("food_kind_name"));
		
		
		PageHelper.startPage((int)map.get("pageNum"), (int)map.get("pageSize"));
		
		List<FoodKind> foodkindlist = foodKindService.getByNameAndId(foodkind);
		
		PageInfo pageinfo =new PageInfo<>(foodkindlist,(int)map.get("pageSize"));
		
		rmap.put("foodkindList", pageinfo);
		
		return rmap;
	}
	
}
